package com.orientation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.orientation.commom.Result;
import com.orientation.dto.AllocateClassParamDto;
import com.orientation.dto.StudentImportDto;
import com.orientation.dto.UserLoginParamDto;
import com.orientation.entity.Student;
import com.orientation.service.StudentService;
import com.orientation.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 学生 前端控制器
 * </p>
 *
 * @author xuandian
 * @since 2022-3-9
 */
@Api(tags = "学生基础信息")
@Validated
@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;

    @ApiOperation("添加")
    @PostMapping
    public ResponseEntity<Result> insertStudent(@RequestBody Student student) {
        studentService.insertStudent(student);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("修改")
    @PutMapping
    public boolean update(@RequestBody Student student) {
        return studentService.updateById(student);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable String id) {
        return studentService.removeById(id);
    }

    @ApiOperation("id查询")
    @GetMapping("/{id}")
    public Student findById(@PathVariable String id) {
        return studentService.getById(id);
    }
//    @ApiOperation("id查询")
//    @GetMapping("/{id}")
//    public StudentPageVo findById(@PathVariable String id) {
//        return studentService.studentClassInfoVo(id);
//    }

    @ApiOperation("列表全查询")
    @GetMapping("/list")
    public List<Student> findAll() {
        return studentService.list();
    }

//    @ApiOperation("分页查询")
//    @GetMapping("/page")
//    public ResponseEntity<Result> findPage(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
//                                           @RequestParam(required = false, defaultValue = "10") Integer pageSize,
//                                           @RequestParam(value = "collegeName", required = false) String collegeName,
//                                           @RequestParam(value = "majorName", required = false) String majorName,
//                                           @RequestParam(value = "studentName", required = false) String studentName,
//                                           @RequestParam(value = "grade", required = false) String grade,
//                                           @RequestParam(value = "domicilePlaceProvince", required = false) String domicilePlaceProvince) {
//        IPage<StudentPageVo> studentVoPage = studentService.findStudentVoPage(new Page<>(pageNum, pageSize), collegeName, majorName, studentName, grade);
//        return ResponseEntity.ok(new Result(studentVoPage));
//    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public ResponseEntity<Result> findPage(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                     @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value = "collegeId", required = false) String collegeId,
                                                     @RequestParam(value = "majorId", required = false) String majorId,
                                                     @RequestParam(value = "studentName", required = false) String studentName,
                                                     @RequestParam(value = "grade", required = false) String grade,
                                                     @RequestParam(value = "domicilePlaceProvince", required = false) String domicilePlaceProvince) {
        IPage<StudentPageVo> studentVoPage = studentService.queryStudentPageVo(pageNum, pageSize, collegeId, majorId, studentName, grade, domicilePlaceProvince);
        return ResponseEntity.ok(new Result(studentVoPage));
    }

    @ApiOperation("学生登录")
    @PostMapping("/login")
    public ResponseEntity<Result> login(@RequestBody UserLoginParamDto loginParamDto) {
        Student ret = studentService.login(loginParamDto);
        return ResponseEntity.ok(Result.success(ret));
    }

    @ApiOperation("导入 废弃不用 模板不正确")
    @PostMapping(value = "import")
    public List<Student> importStudentInfos(MultipartFile file) {
        return studentService.importStudentInfos(file);
    }

    @ApiOperation("导入学生基本信息 ")
    @PostMapping("/importStudentPileInfo")
    public ResponseEntity<Result> importStudentPileInfo(MultipartFile file) {
        studentService.importStudentPileInfo(file);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("导入学生基本信息 废弃不用")
    @PostMapping("/importPileInfo")
    public ResponseEntity<Result> importPileInfo(MultipartFile file) {
        studentService.importPileInfo(file);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("新增单个学生数据 废弃不用")
    @PostMapping("/insertStudent")
    public ResponseEntity<Result> insertStudent(@RequestBody StudentImportDto studentImportDto) {
        studentService.insertStudentList(studentImportDto);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("获取未分班级专业人数 ")
    @GetMapping("/studentMajor")
    public ResponseEntity<Result> insertStudent() {
        List<MajorSizeVo> majorSizeVos = studentService.studentMajor();
        return ResponseEntity.ok(new Result(majorSizeVos));

    }

    @ApiOperation("获取已经分配班级列表")
    @GetMapping("/queryStudentClass")
    public ResponseEntity<Result> queryStudentClass(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                    @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                    @RequestParam(value = "collegeId", required = false) String collegeId,
                                                    @RequestParam(value = "majorId", required = false) String majorId,
                                                    @RequestParam(value = "className") String className,
                                                    @RequestParam(value = "studentName", required = false) String studentName,
                                                    @RequestParam(value = "allocateDate", required = false) String allocateDate,
                                                    @RequestParam(defaultValue = "") String adviserId) {
        IPage<StudentClassInfoVo> studentClassInfoVoIPage = studentService.queryStudentClass(pageNum, pageSize, collegeId, majorId, className, studentName, allocateDate, adviserId);
        return ResponseEntity.ok(Result.success(studentClassInfoVoIPage));
    }

    @ApiOperation("获取已经存在学号学生列表分配床位")
    @GetMapping("/queryStudentIdBed")
    public ResponseEntity<Result> queryStudentIdBed(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                    @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                    @RequestParam(value = "collegeId", required = false) String collegeId,
                                                    @RequestParam(value = "majorId", required = false) String majorId,
                                                    @RequestParam(value = "className") String className,
                                                    @RequestParam(value = "studentName", required = false) String studentName,
                                                    @RequestParam(value = "sexType", required = true, defaultValue = "1") String sexType,
                                                    @RequestParam(value = "adviserId", defaultValue = "") String adviserId) {
        IPage<StudentClassInfoVo> studentClassInfoVoIPage = studentService.queryStudentIdBed(pageNum, pageSize, collegeId, majorId, className, studentName, sexType, adviserId);
        return ResponseEntity.ok(Result.success(studentClassInfoVoIPage));
    }

    @ApiOperation("获取已经分配班级列表导出")
    @GetMapping("/statisticsExport")
    public void statisticsExport(@RequestParam(value = "collegeId") String adviserId,
                                 @RequestParam(value = "majorId", required = false) String majorId,
                                 @RequestParam(value = "className", required = false) String className,
                                 @RequestParam(value = "studentName", required = false) String studentName,
                                 @RequestParam(value = "allocateDate", required = false) String allocateDate,
                                 HttpServletResponse response) {
        studentService.statisticsExport(adviserId, majorId, className, studentName, allocateDate, response);

    }

    //    @ApiOperation("已经分配班级学生导出")
    //    @GetMapping("/queryStudentClass/export")
    //
    //    public ResponseEntity<Result> statisticsByProjectExport(@RequestParam(value = "collegeId", required = false) String collegeId,
    //                                                            @RequestParam(value = "majorId", required = false) String majorId,
    //                                                            @RequestParam(value = "className") String className,
    //                                                            @RequestParam(value = "studentName", required = false) String studentName,
    //                                                            @RequestParam(value = "allocateDate", required = false) String allocateDate,
    //                                                            HttpServletResponse response) {
    //        studentService.statisticsByUnitExport(collegeId, majorId, className, studentName, allocateDate, response);
    //        return ResponseEntity.ok(new Result());
    //    }

    @ApiOperation("未分配班级分页查询 ")
    @GetMapping("/pageAlloc")
    public ResponseEntity<Result> queryNullClassId(@RequestParam(required = false, defaultValue = "") String majorId,
                                                   @RequestParam(required = false, defaultValue = "") String name,
                                                   @RequestParam(defaultValue = "") String adviserId,
                                                   @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                   @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        IPage<Student> studentIPage = studentService.queryNullClassId(pageNum, pageSize, majorId, name, adviserId);
        return ResponseEntity.ok(new Result(studentIPage));

    }

    @ApiOperation("已经分配班级学生分页查询  废弃不用")
    @GetMapping("/queryNotNullClassId")
    public ResponseEntity<Result> queryNotNullClassId(@RequestParam(required = false, defaultValue = "") String majorId,
                                                      @RequestParam(required = false, defaultValue = "") String name,
                                                      @RequestParam(defaultValue = "") String adviserId,
                                                      @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                      @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        IPage<Student> studentIPage = studentService.queryNotNullClassId(pageNum, pageSize, majorId, name, adviserId);
        return ResponseEntity.ok(new Result(studentIPage));

    }

    @ApiOperation("分配班级")
    @PostMapping("/allocateClass")
    public ResponseEntity<Result> allocateClass(@RequestBody AllocateClassParamDto classParamDto) {
        studentService.allocateClass(classParamDto);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("移除班级")
    @PostMapping("/updateClassId")
    public ResponseEntity<Result> updateClassId(@RequestBody AllocateClassParamDto classParamDto) {
        studentService.updateClassId(classParamDto);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("微调班级")
    @PostMapping("/updateStudentClass")
    public ResponseEntity<Result> updateStudentClass(@RequestBody AllocateClassParamDto classParamDto) {
        studentService.updateStudentClass(classParamDto);
        return ResponseEntity.ok(Result.success());
    }


    @ApiOperation("按省份统计学生")
    @GetMapping("/statProvince")
    public ResponseEntity<Result> queryStudentProvinceGroup(String collegeId, String majorId) {
        List<PieDataVo> studentProvinceGroup = studentService.getStudentProvinceGroup(collegeId, majorId);
        return ResponseEntity.ok(Result.success(studentProvinceGroup));
    }

    @ApiOperation("已经分配班级文件导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        studentService.export(response);
    }

    @ApiOperation("获取已经存在学号学生列表//物资领取")
    @GetMapping("/queryStudentIdGoods")
    public ResponseEntity<Result> queryStudentIdGoods(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                      @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                      @RequestParam(value = "collegeId", required = false) String collegeId,
                                                      @RequestParam(value = "majorId", required = false) String majorId,
                                                      @RequestParam(value = "className") String className,
                                                      @RequestParam(value = "studentName", required = false) String studentName) {
        IPage<StudentClassInfoVo> studentClassInfoVoIPage = studentService.queryStudentIdGoods(pageNum, pageSize, collegeId, majorId, className, studentName);
        return ResponseEntity.ok(Result.success(studentClassInfoVoIPage));
    }

    @ApiOperation("报道单")
    @GetMapping("/studentReport")
    public ResponseEntity<Result> studentReport(@RequestParam(value = "collegeId") String studentId, @RequestParam(value = "idCardNo", required = false) String idCardNo) {
        ReportSheetVo reportSheetVo = studentService.studentReport(studentId, idCardNo);
        return ResponseEntity.ok(Result.success(reportSheetVo));
    }

    @ApiOperation("未报到学生")
    @GetMapping("/fileCheckIn")
    public ResponseEntity<Result> fileCheckIn(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                              @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                              @RequestParam(value = "majorId", required = false) String majorId,
                                              @RequestParam(value = "className", required = false) String className,
                                              @RequestParam(value = "studentName", required = false) String studentName,
                                              @RequestParam(defaultValue = "") String adviserId) {
        IPage<StudentClassInfoVo> studentClassInfoVoIPage = studentService.fileCheckIn(pageNum, pageSize, majorId, className, studentName, adviserId);
        return ResponseEntity.ok(Result.success(studentClassInfoVoIPage));
    }

    @ApiOperation("未报到级列表导出")
    @GetMapping("/fileCheckInExport")
    public void fileCheckInExport(@RequestParam(value = "majorId", required = false) String majorId,
                                  @RequestParam(value = "className", required = false) String className,
                                  @RequestParam(value = "studentName", required = false) String studentName,
                                  @RequestParam(defaultValue = "") String adviserId,
                                  HttpServletResponse response) {
        studentService.fileCheckInExport(majorId, className, studentName, adviserId, response);

    }

    @ApiOperation("未报到学生All")
    @GetMapping("/queryfileCheckInAll")
    public ResponseEntity<Result> queryfileCheckInAll(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                              @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                              @RequestParam(value = "id", required = false) String id,
                                              @RequestParam(value = "studentId", required = false) String studentId,
                                              @RequestParam(value = "studentName", required = false) String studentName) {
        IPage<StudentClassInfoVo> studentClassInfoVoIPage = studentService.queryfileCheckInAll(pageNum, pageSize, id, studentId, studentName);
        return ResponseEntity.ok(Result.success(studentClassInfoVoIPage));
    }

}

